package com.iule.common.playboy.config;

import com.iule.common.playboy.interceptor.IpInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Default webConfig
 * @author 柚子
 * @date {2020/3/18}
 */
@Configuration
public class BaseWebConfig implements WebMvcConfigurer {

    private IpInterceptor ipInterceptor;

    @Autowired
    private void setIpInterceptor(IpInterceptor ipInterceptor) {
        this.ipInterceptor = ipInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(ipInterceptor);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}
